//
//  DDHTTPSessionManagerTool.swift
//  swift微博
//
//  Created by whitcast on 16/7/12.
//  Copyright © 2016年 whitcast. All rights reserved.
//

import UIKit
import AFNetworking

//请求方式的枚举
enum requestWay: Int {
    case POST
    case GET
}

class DDHTTPSessionManagerTool: AFHTTPSessionManager {
    //单例对象
    static let shardTool : DDHTTPSessionManagerTool = {
      let tool = DDHTTPSessionManagerTool()
        
        tool.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tool
    
    }()
    //请求方法
    
    func RequsetWithAFN(requestWays :requestWay,url: String , pramas : [String :AnyObject],callback :((response:AnyObject? ,error :NSError? )->())?) {
    //根据枚举值判断请求方式
        if requestWays == .GET {
            
            DDHTTPSessionManagerTool.shardTool.GET(url, parameters: pramas, progress: nil, success: { (_, response) -> Void in
                //回调block
                callback?(response: response , error: nil)
                
                }, failure: { (_, error) -> Void in
                    //回调block
                    callback?(response: nil, error: error)
                    
            })
            
        }else {
            
            DDHTTPSessionManagerTool.shardTool.POST(url, parameters: pramas, progress: nil, success: { (_, response) -> Void in
                
                //回调block
                callback!(response: response , error: nil)
                
                }, failure: { (_, error) -> Void in
                    
                    //回调block
                    callback!(response: nil, error: error)

                    
            })
        }

    }
    
    //获取授权码相关接口
    func getCode (code : String ,callBack : (response :AnyObject?,error :NSError? )->()) {
        // MARK:---根据授权码加载数据
            let url = "https://api.weibo.com/oauth2/access_token"
            
            let prama : [String :AnyObject]? = [
                "client_id" : client_id ,//appkey
                "client_secret" : weiboAppSecret  ,//appsecret
                "grant_type" : "authorization_code",
                "code" : code ?? "", //授权码
                "redirect_uri": redirect_uri //回调地址
            ]
        
            DDHTTPSessionManagerTool.shardTool.RequsetWithAFN(.POST, url: url, pramas: prama!, callback: callBack)
  
    }
    
    //获取用户数据
    func getUserAccount(model :DDUserAccountModel,callBack: (response :AnyObject?,error :NSError?)->()) {
        let url = "https://api.weibo.com/2/users/show.json"
        //请求参数
        let prama = [
            "access_token" : model.access_token!,
            
            "uid" : model.uid
        ]
        
        print("https://api.weibo.com/2/users/show.json?"+"access_token=\(model.access_token!)&uid=\(model.uid)")
        
        DDHTTPSessionManagerTool.shardTool.RequsetWithAFN(.GET, url: url, pramas: prama as! [String : AnyObject], callback: callBack)

    }
}

//MARK:--发送微博
extension DDHTTPSessionManagerTool{
    //发送文字
    func sendText(text: String,callBack:(response :AnyObject?,error :NSError?)->()){
        let urlString = "https://api.weibo.com/2/statuses/update.json"
        let prama: [String: AnyObject] = [
                        "access_token": (DDUserAccountModel.getDataFormLocal()?.access_token)!,
                        "status": text
        ]
        
        DDHTTPSessionManagerTool.shardTool.RequsetWithAFN(.POST, url: urlString, pramas: prama, callback: callBack)
        
    }
    //发送文字和图片
    func sendPictureAndText(urlString: String,prama: [String: AnyObject],image: UIImage,callBack:(response :AnyObject?,error :NSError?)->()){
        DDHTTPSessionManagerTool.shardTool.POST(urlString, parameters: prama, constructingBodyWithBlock: { (formData) -> Void in
            //添加图片数据
            let imageData = UIImagePNGRepresentation(image)
            
            formData.appendPartWithFileData(imageData!, name: "pic", fileName: "img", mimeType: "octet-stream")
            }, progress: nil, success: { (_, response) -> Void in
                //成功回调
                callBack(response: response,error: nil)
                
            }) { (_, error) -> Void in
                //失败回调
                callBack(response: nil,error: error)
                
        }
    }
    
    func sendPicAndText(text: String ,image: UIImage, callBack: (response :AnyObject?,error :NSError?)->()){
        let urlstr = "https://upload.api.weibo.com/2/statuses/upload.json"
        let pramas = ["access_token":(DDUserAccountModel.getDataFormLocal()?.access_token)!,"status":text]
        
        DDHTTPSessionManagerTool.shardTool.sendPictureAndText(urlstr, prama: pramas, image: image, callBack: callBack)
    }
    
}


